package com.chandra.pos.order.service.impl;

import com.chandra.pos.common.model.TransactionType;
import com.chandra.pos.customer.dao.entity.TransactionEntity;
import com.chandra.pos.customer.model.CustomerFilter;
import com.chandra.pos.customer.model.CustomerModel;
import com.chandra.pos.customer.service.CustomerRole;
import com.chandra.pos.customer.service.TransactionRole;
import com.chandra.pos.order.dao.entity.OrderStatus;
import com.chandra.pos.order.dao.entity.ShippingStatus;
import com.chandra.pos.order.model.*;
import com.chandra.pos.order.service.OrderRole;
import com.chandra.pos.order.service.OrderService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityNotFoundException;
import javax.transaction.Transactional;

/**
 * Created by dnagarajan on 3/28/2015.
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderRole orderRole;


    @Autowired
    private CustomerRole customerRole;


    @Autowired
    private TransactionRole transactionRole;

    @Override
    @Transactional
    public OrderModel saveOrder(OrderModel orderModel) {

        OrderModel orderModel1= orderRole.saveOrder(orderModel);
        TransactionEntity transactionEntity =new TransactionEntity();
        transactionEntity.setCustomerId(orderModel.getCustomerId());
        transactionEntity.setTransactionType(TransactionType.SO);
        transactionEntity.setTransactionAmount(orderModel.getTotalOrderAmount());
        transactionEntity.setTransactionDetailId(orderModel.getOrderId());
        transactionEntity.setCustomerId(orderModel.getCustomerId());
        transactionEntity.setCustomerName(orderModel.getCustomerName());


        transactionRole.saveTransaction(transactionEntity);

        return orderModel;

    }

    @Override
    @Transactional
    public OrderItemModel saveOrderItem(OrderItemModel orderItemModel) {

        if (orderItemModel.getOrderId() == null || orderItemModel.getOrderId() == 0) {
            OrderModel orderModel = new OrderModel();
            orderModel.setOrderSubTotalAmount(orderItemModel.getTotal());
            orderModel.setTotalOrderAmount(orderItemModel.getTotal());
            orderModel.setOrderDate(DateTime.now());
            orderModel.setStatus(OrderStatus.IPS);
            orderModel.setOrderDueAmount(orderItemModel.getTotal());
            orderModel.setOrderDiscount(0d);
            orderModel.setOrderShippingAmount(0d);
            orderModel.setShippingStatus(ShippingStatus.PE);
            orderModel = orderRole.saveOrder(orderModel);

            orderItemModel.setOrderId(orderModel.getOrderId());
        } else {
            OrderModel orderModel = orderRole.getOrderById(orderItemModel.getOrderId());
            orderModel.setOrderSubTotalAmount(orderModel.getOrderSubTotalAmount()+ orderItemModel.getTotal());
            orderModel.setTotalOrderAmount(orderModel.getTotalOrderAmount() + orderItemModel.getTotal());
            orderModel.setOrderDueAmount(orderModel.getOrderDueAmount() + orderItemModel.getTotal());
            orderRole.saveOrder(orderModel);

        }
        return orderRole.saveOrderItem(orderItemModel);
    }

    @Override
    @Transactional
    public boolean removeOrderItem(OrderItemModel orderItemModel) {
        if(orderItemModel.getOrderId() ==null)
            return false;

        OrderModel orderModel = orderRole.getOrderById(orderItemModel.getOrderId());
        orderModel.setOrderSubTotalAmount(orderModel.getOrderSubTotalAmount() - orderItemModel.getTotal());
        orderModel.setTotalOrderAmount(orderModel.getTotalOrderAmount() - orderItemModel.getTotal());
        orderModel.setOrderDueAmount(orderModel.getOrderDueAmount() -orderItemModel.getTotal());
        orderRole.saveOrder(orderModel);



        return orderRole.removeOrderItem(orderItemModel);
    }

    @Override
    public OrderList search(OrderFilter model) {
        return orderRole.search(model);
    }

    @Override
    public OrderItemList listOrderItems(OrderItemModel orderItemModel) {
        return orderRole.listOrderItems(orderItemModel);

    }

    @Override
    public OrderModel getOrderDetails(OrderFilter filter) throws EntityNotFoundException {


        OrderModel orderModel= orderRole.getOrderDetails(filter);

        if(orderModel.getCustomerId() !=null)
        {
            CustomerFilter customerFilter=new CustomerFilter(orderModel.getCustomerId());

            CustomerModel customerModel=customerRole.getCustomerDetails(customerFilter);
            orderModel.setCustomer(customerModel);


        }
        return  orderModel;

    }


}
